package com.wzgl.core.controller;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.wzgl.core.base.JBaseController;
import com.wzgl.core.interceptor.RequiredUserIdInterceptor;
import com.wzgl.core.model.Tdicinfo;
import com.wzgl.core.model.Tmagic;
import com.wzgl.core.model.Trelease;
import com.wzgl.core.model.Tusers;
import com.wzgl.core.utils.DateUtils;
import com.wzgl.core.utils.UuidUtil;

public class ReleaseSetingController extends JBaseController {
	/***
	 * 公众号设置
	 */
	@Before(RequiredUserIdInterceptor.class)
	public void WeChatIndex(){
		render("wechat_list.jsp");
	}
	
	@Before(RequiredUserIdInterceptor.class)
	public void release_eit(){
		String fid = getPara("fid");
		Trelease t =null;
		List<Tdicinfo> tdic = Tdicinfo.dao.getTdicBySort(PropKit.use("wzgl_tdinc_conts.txt").get("tdic_wzlx"));
		List<Tdicinfo> timers = Tdicinfo.dao.getTdicBySort(PropKit.use("wzgl_tdinc_conts.txt").get("tdic_timers"));
		setAttr("tdic", tdic);
		setAttr("timers", timers);
		if(StrKit.notBlank(fid)){
			t = Trelease.dao.findById(fid);
			setAttr("trelease", t);
		}
		render("wechat_edit.jsp");
	}
	/***
	 * 保存公众号信息
	 */
	@Before(RequiredUserIdInterceptor.class)
	public void treleaseSave(){
		final Trelease t = getModel(Trelease.class);
		if(StrKit.notBlank(getParaValues("trelease.ftype").toString())){
			String [] str = getParaValues("trelease.ftype");
			StringBuffer sb = new StringBuffer();
			for(String s:str){
				sb.append(s).append(",");
			}
			t.setFtype(sb.toString());
		}
		if (t != null && StrKit.isBlank(t.getFid())) {
			Trelease temp = Trelease.dao.findByAppId(t.getFappId());
			if(temp != null){
				renderJson(Ret.fail("msg", "微信APPID已经存在,确认是否已登记!"));
			}
			return;
		}
		boolean saved = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {
				if(StrKit.notBlank(t.getFid())){
					if(!t.update()){
						return false;
					}
				}else{
					t.setFid(UuidUtil.get32UUID());
					t.setFctime(new Date());
					if(!t.save()){
						return false;
					}
				}
//				user.putCache(user.CACHE_NAME,user);
				return true;
			}
		});
		if(saved){
			renderJson(Ret.ok());
		}else{
			renderJson(Ret.fail("msg", "保存失败,请稍后再试!"));
		}
	}
	/***
	 * 获取公众号数据
	 */
	@Before(RequiredUserIdInterceptor.class)
	public void WeChatList(){
		Page<Trelease> list_page;
		try {
			BigInteger page=getParaToBigInteger("page",BigInteger.valueOf(1));
			BigInteger pagesize=getParaToBigInteger("pagesize",BigInteger.valueOf(5));
			String fvalue =  getParaTo("fvalue");
			Trelease t = getModel(Trelease.class);
//			t.setFstatus(0);
			t.setFdesc(fvalue);
			list_page= Trelease.dao.paginate(page.intValue(),pagesize.intValue(),t);
			renderAjaxResult("",0,list_page);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			renderAjaxResultForError(e.getMessage());
		}
	}
}
